/*
 * Syntelos 'SX'
 * Copyright (C) 1998, 2007  John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package syntelos.sx.methods.s;

import syntelos.sx.Response;
import syntelos.sx.Request;
import syntelos.sys.Capabilities;

import alto.io.Authentication;
import alto.io.Check;
import alto.io.Code;
import alto.io.Output;
import alto.io.Principal;
import alto.lang.Buffer;
import alto.lang.Component;
import alto.lang.Type;
import alto.sec.Keys;
import alto.sec.pkcs.PKCS12;
import alto.sec.x509.X500Name;
import alto.sec.x509.X509Certificate;


/**
 * Get public user certificate.  A correct request path is one of
 * <pre>
 * /s/identifier.der
 * /s/identifier.txt
 * /s/identifier.x500-name
 * /s/identifier.pfx
 * </pre>
 * 
 * The 'der' file is a X.509 binary public key certificate, the 'txt'
 * file is Base64 encoded public key certificate. 
 * 
 * The 'x500-name' file returns the RFC 2253 textual representation
 * (DN) of the X.500 name as in the following example.
 * <pre>
 * CN=Bill Joy, O=Sun Microsystems, L=Mountain View, S=CA, C=US
 * </pre>
 * 
 * The 'pfx' file requires authentication.  It is the full RSA key
 * set in PKCS#12 format.
 * 
 * @since 1.6
 */
public class Get
    extends Abstract
{

    public Get(){
        super();
    }


    public void respond(Request req)
        throws java.io.IOException,
               java.lang.InterruptedException
    {
        super.respond(req);
        if (req.hasType()){
            String uid = IdentifierFrom(req);
            if (null != uid){
                /*
                 * Derefence anyone's Keys in order to return public
                 * info without needing authentication.
                 */
                Keys keys = Keys.Tools.Dereference(uid);
                if (null != keys){
                    Output buf = this.openOutput();
                    try {
                        Type type = req.getType();
                        if (Type.Tools.Of("pfx") == type){
                            if (this.isAuthenticated(Authentication.NotApp, 
                                                     Capabilities.UserKeys.Instance,
                                                     keys.getPrincipal()))
                            {
                                String passwd = req.getParameter("password");
                                if (null != passwd){
                                    try {
                                        PKCS12 pfx = keys.createPfx("RSA",passwd);
                                        pfx.encode((java.io.OutputStream)buf,passwd);
                                        this.setContentType(type);
                                        this.setStatusOk();
                                    }
                                    catch (java.security.cert.CertificateException exc){
                                        throw new alto.sys.Error(exc);
                                    }
                                }
                                else
                                    this.setStatusBadRequest("Bad request, missing Password");
                            }
                            else
                                this.setStatusUnauthorized();
                        }
                        else if (Type.Tools.Of("der") == type){
                            X509Certificate cert = keys.getCertificate("RSA");
                            if (null != cert){
                                cert.derEncode((java.io.OutputStream)buf);
                                this.setContentType(type);
                                this.setStatusOk();
                            }
                            else
                                this.setStatusNotFound();
                        }
                        else if (Type.Tools.Of("txt") == type){
                            X509Certificate cert = keys.getCertificate("RSA");
                            if (null != cert){
                                cert.b64Encode((java.io.OutputStream)buf);
                                this.setContentType(type);
                                this.setStatusOk();
                            }
                            else
                                this.setStatusNotFound();
                        }
                        else if (Type.Tools.Of("x500-name") == type){
                            X500Name name = keys.getX500Name();
                            if (null != name){
                                String dn = name.getRFC2253Name();
                                buf.println(dn);
                                this.setContentTypeText();
                                this.setStatusOk();
                            }
                            else
                                this.setStatusNotFound();
                        }
                        else
                            this.setStatusBadRequest("Bad request, missing Type");
                    }
                    finally {
                        buf.close();
                    }
                }
                else
                    this.setStatusNotFound();
            }
            else
                this.setStatusBadRequest("Bad request, missing Path");
        }
        else
            this.setStatusBadRequest("Bad request, missing Type");
    }

}
